package console

import (
	"duoke/model"
	"duoke/service"
	"duoke/utils/gconv"
	"duoke/utils/gjwt"
	"duoke/utils/gstr"
	"duoke/utils/response"
	"duoke/utils/valid"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
)

var Account = new(accountCtl)

type accountCtl struct{}

// 账户登录
func (ctl *accountCtl) Login(c *gin.Context) {
	var req model.ReqAccountLogin
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, valid.GetError(err.(validator.ValidationErrors), req), nil)
		return
	}
	// 系统登录
	account, err := service.Account.Login(req.Username, req.Password)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	// 生成token
	token, err := gjwt.Build(gjwt.Data{Uid: account.Id, Role: account.Role, Pid: account.Pid, GoodsIds: account.BindGoodsIds}, 3600*24*30)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "登录成功", gin.H{"token": token})
}

// 账户信息
func (ctl *accountCtl) Info(c *gin.Context) {
	accountR, _ := service.Request.Account(c)
	account, err := service.Account.GetInfo(accountR.Id)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "", account)
}

// 账户保存
func (ctl *accountCtl) Save(c *gin.Context) {
	accountR, _ := service.Request.Account(c)
	var req model.ReqAccount
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, valid.GetError(err.(validator.ValidationErrors), req), nil)
		return
	}
	// 过期时间预处理
	req.ExpireTime = gconv.Int64(gstr.SubStr(gconv.String(req.ExpireTime), 0, 10))
	req.Role = "account"
	if req.UseStorageAcid > 0 {
		req.UseStorageAcid = accountR.Id
	} else {
		req.UseStorageAcid = 0
	}
	// 保存数据
	err := service.Account.Save(c, req)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "保存成功", nil)
}

// 账户保存
func (ctl *accountCtl) UpdateInfo(c *gin.Context) {
	accountR, _ := service.Request.Account(c)
	var req model.ReqAccountInfo
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, valid.GetError(err.(validator.ValidationErrors), req), nil)
		return
	}
	err := service.Account.SaveInfo(accountR.Id, req)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "保存成功", nil)
}

// 账户登出
func (ctl *accountCtl) Logout(c *gin.Context) {
	response.Success(c, "", nil)
}

// 账户列表
func (ctl *accountCtl) List(c *gin.Context) {
	var req model.ReqList
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	list, total, err := service.Account.List(req)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "", gin.H{"total": total, "list": list})
}

// 账户删除
func (ctl *accountCtl) Del(c *gin.Context) {
	var req model.ReqIds
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, valid.GetError(err.(validator.ValidationErrors), req), nil)
		return
	}
	err := service.Account.Del(req)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "删除成功", nil)
}

// 账户状态
func (ctl *accountCtl) State(c *gin.Context) {
	var req model.ReqId
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, valid.GetError(err.(validator.ValidationErrors), req), nil)
		return
	}
	err := service.Account.State(req)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "操作成功", nil)
}
